import React from 'react';
import NVD3Chart from 'react-nvd3';
import { API_SERVER } from './../../../../config/constant';
import axios from 'axios';
function generateNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

function getDatum() {
    let sin = [],
        sin2 = [],
        sin3 = [];

    const len = 35 + Math.random() * (70 - 35);
    for (let i = 0; i < len; i++) {
        sin.push({
            x: i,
            y: generateNumber(0, 60)
        });
        sin2.push({
            x: i,
            y: generateNumber(0, 100)
        });
        sin3.push({
            x: i,
            y: generateNumber(0, 30)
        });
    }
    return [
        {
            values: sin,
            key: 'Stream #0',
            color: '#A389D4'
        },
        {
            values: sin3,
            key: 'Stream #1',
            color: '#04a9f5'
        },
        {
            values: sin2,
            key: 'Stream #3',
            color: '#1de9b6',
            area: true
        }
    ];
}

const sin=[],sin2=[],sin3=[],sin4=[];
function getDatum2() {

    var stockname="600000";
    


    axios.post(API_SERVER + 'stocks/getdata', {
            "stockname":stockname
        })
        .then(function (response) {      
            stockname=response.data.stockname;
            for (var i = 0; i < response.data.data.length; i++) {
                if(response.data.pctchg[i]<2&&response.data.pctchg[i]>0){
                    sin.push(response.data.pctchg[i]);

                }else if(response.data.pctchg[i]>-2&&response.data.pctchg[i]<0){
                    sin2.push(response.data.pctchg[i]);
                }
                else if(response.data.pctchg[i]<-2){
                    sin3.push(response.data.pctchg[i]);
                }else{
                    sin4.push(response.data.pctchg[i]);
                }
            }
            if (response.data.success) {
                console.log(response.data);
                //alert(response.data)
            }
        })
        .catch(function (error) {
            console.log(error);
        });
            
    return [
        {
            key: 'Cumulative Return',
            values: [
                {
                    label: 'A',
                    value: sin.length,
                    color: '#3ebfea'
                },
                {
                    label: 'B',
                    value: sin2.length,
                    color: '#04a9f5'
                },
                {
                    label: 'C',
                    value: sin3.length,
                    color: '#ff8a65'
                },
                {
                    label: 'D',
                    value: sin4.length,
                    color: '#1de9b6'
                }
            ]
        }
    ];
}
const data = getDatum2();
// const datum = [
//     {
//         key: 'Cumulative Return',
//         values: [
//             {
//                 label: 'A',
//                 value: a,
//                 color: '#3ebfea'
//             },
//             {
//                 label: 'B',
//                 value: b,
//                 color: '#04a9f5'
//             },
//             {
//                 label: 'C',
//                 value: c,
//                 color: '#ff8a65'
//             },
//             {
//                 label: 'D',
//                 value: d,
//                 color: '#1de9b6'
//             }
//         ]
//     }
// ];

const BarDiscreteChart = () => {
    return <NVD3Chart tooltip={{ enabled: true }} type="discreteBarChart" datum={data} x="label" y="value" height={300} showValues />;
};

export default BarDiscreteChart;
